package com.dcbeida;

import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;

import com.dcbeida.activity.BaseActivity;
import com.dcbeida.activity.HomeActivity;
import com.dcbeida.activity.clerkmanager.AccountListActivity;
import com.dcbeida.activity.login.LoginActivity;
import com.dcbeida.activity.login.LoginWithCodeActivity;
import com.dcbeida.bean.User;
import com.dcbeida.db.AreaDB;
import com.dcbeida.listern.AlertDialogListener;
import com.dcbeida.utils.Constants;
import com.dcbeida.utils.DialogUtils;
import com.dcbeida.utils.SharedPreferencesUtils;
import com.dcbeida.utils.permission.AndPermission;
import com.dcbeida.utils.permission.PermissionListener;
import com.dcbeida.utils.permission.Rationale;
import com.dcbeida.utils.permission.RationaleListener;

import java.util.List;

import static com.dcbeida.utils.Constants.PERMISSION_ACCESS_COARSE_LOCATION;
import static com.dcbeida.utils.Constants.PERMISSION_ACCESS_FINE_LOCATION;
import static com.dcbeida.utils.Constants.PERMISSION_CALL_PHONE;
import static com.dcbeida.utils.Constants.PERMISSION_CAMERA;
import static com.dcbeida.utils.Constants.PERMISSION_GET_ACCOUNTS;
import static com.dcbeida.utils.Constants.PERMISSION_READ_EXTERNAL_STORAGE;
import static com.dcbeida.utils.Constants.PERMISSION_READ_PHONE_STATE;
import static com.dcbeida.utils.Constants.PERMISSION_RECORD_AUDIO;
import static com.dcbeida.utils.Constants.PERMISSION_WRITE_EXTERNAL_STORAGE;

public class LoadingActivity extends BaseActivity {
    private boolean isShowDialog = true;
    private static final String[] requestPermissions = {
            PERMISSION_CALL_PHONE,
            PERMISSION_CAMERA,
            PERMISSION_READ_EXTERNAL_STORAGE,
            PERMISSION_WRITE_EXTERNAL_STORAGE
    };

    Handler handler = new Handler();

    private RationaleListener rationaleListener = new RationaleListener() {
        @Override
        public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
            rationale.resume();
        }
    };

    private PermissionListener listener = new PermissionListener() {
        @Override
        public void onSucceed(int requestCode, List<String> grantPermissions) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(MyApplication.user ==null){
                        showActivity(LoginWithCodeActivity.class, null);
                    }else {
                        Log.i("fancl","onSucceed");
                        showActivity(HomeActivity.class,null);
                    }
                    finish();
                }
            }, 0);
        }

        @Override
        public void onFailed(int requestCode, List<String> deniedPermissions) {
            Log.i("swt", "onFailed = " + isShowDialog);
            isShowDialog = true;
            DialogUtils.showAlertDialog(mContext, "", getString(R.string.permissionstr), getString(R.string.confim), getString(R.string.close), new AlertDialogListener() {
                @Override
                public void onCancl(DialogInterface dialog, int which) {
                    isShowDialog = false;
                    dialog.dismiss();
                    finish();
                    android.os.Process.killProcess(android.os.Process.myPid());
                }

                @Override
                public void onConfirm(DialogInterface dialog, int which) {
                    //权限申请
                    isShowDialog = false;
                    dialog.dismiss();

                }
            });
        }
    };
    private boolean checkSelfPermission;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        //init();
        //权限申请
        AndPermission.with(LoadingActivity.this)
                .requestCode(100)
                .permission(requestPermissions)
                .rationale(rationaleListener)
                .send();


    }

    @Override
    protected void onResume() {


//        if ((PackageManager.PERMISSION_GRANTED == getPackageManager().checkPermission("android.permission.CAMERA", getPackageName()))
//                && (PackageManager.PERMISSION_GRANTED == getPackageManager().checkPermission("android.permission.CALL_PHONE", getPackageName()))
//                && (PackageManager.PERMISSION_GRANTED == getPackageManager().checkPermission("android.permission.READ_EXTERNAL_STORAGE", getPackageName()))
//                && (PackageManager.PERMISSION_GRANTED == getPackageManager().checkPermission("android.permission.WRITE_EXTERNAL_STORAGE", getPackageName()))
//                ) {
//            checkSelfPermission = true;
//        }
//
//        Log.i("swt", "onResume = " + checkSelfPermission);
//        if (checkSelfPermission) {
//            if(MyApplication.user ==null){
//                showActivity(LoginWithCodeActivity.class, null);
//            }else {
//                Log.i("fancl","checkSelfPermission");
//                showActivity(HomeActivity.class,null);
//            }
//            finish();
//        } else {
//            if (!isShowDialog) {
//                DialogUtils.showAlertDialog(mContext, "", getString(R.string.permissionstr), getString(R.string.confim), getString(R.string.close), new AlertDialogListener() {
//                    @Override
//                    public void onCancl(DialogInterface dialog, int which) {
//                        isShowDialog = false;
//                        dialog.dismiss();
//                        finish();
//                        android.os.Process.killProcess(android.os.Process.myPid());
//                    }
//
//                    @Override
//                    public void onConfirm(DialogInterface dialog, int which) {
//                        isShowDialog = false;
//                        //权限申请
//                        dialog.dismiss();
//
//                    }
//                });
//            }
//        }
//
//
//        isShowDialog = true;

        super.onResume();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        AndPermission.onRequestPermissionsResult(requestCode, permissions, grantResults, listener);
    }

    @Override
    protected int getLayoutResId() {

        return R.layout.activity_loading;
    }

}
